import 'package:flutter/material.dart';

import '../common/flatPanel.dart';
import '../controller/changeLogController.dart';

class ChangeLogPage extends StatefulWidget {
  ChangeLogPage(
      {Key key,
      @required this.title,
      @required this.table,
      @required this.configName})
      : super(key: key);
  final String title;
  final String table;
  final String configName;

  @override
  _ChangeLogPageState createState() => _ChangeLogPageState();
}

class _ChangeLogPageState extends State<ChangeLogPage>
    with ChangeLogController {
  @override
  void initState() {
    super.initState();
    mTable = widget.table;
  }

  @override
  Future<void> dispose() async {
    super.dispose();
    await disposeAction();
  }

  @override
  Future<void> onUpdateTopics() async {
    await updateTopics();
    setState(() {
      mViewState = ViewState.DetailView;
    });
  }

  @override
  Future<void> onUpdateView() async {
    await updateViewAction();
    setState(() {
      mViewState = ViewState.DetailView;
    });
  }

  @override
  Future<void> onAddItem() async {
    await addItemAction(context);
  }

  @override
  Future<void> onEditItem() async {
    await editItemAction(context);
  }

  @override
  Future<void> onDeleteItem() async {
    await deleteItemAction();
  }

  @override
  Future<void> onTapItem() async {
    if (mItem.id == placeHolder) {
      placeHolder = -1;
      mViewState = ViewState.DetailView;
    } else {
      placeHolder = mItem.id;
      mViewState = ViewState.CheckedView;
    }
    setState(() {});
  }

  @override
  Future<void> onConnectDB() async {
    await connectDbAction(context, widget.configName, onUpdateTopics);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title), actions: <Widget>[
        genPopupMenuButtonAction(context),
      ]),
      body: FlatPanel(
        child: Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: genTopicViewAction(style: TextStyle(fontSize: 18)),
            ),
            Expanded(
              flex: 3,
              child: genListViewAction(),
            ),
          ],
        ),
      ),
      floatingActionButton: genFloatingActionButtonAction(),
    );
  }
}
